package ytdp.careagle.com.ytdp.net;

import android.content.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.entity.BaseConfig;
import ytdp.careagle.com.ytdp.entity.BrandRelationship;
import ytdp.careagle.com.ytdp.entity.LoginEntity;
import ytdp.careagle.com.ytdp.entity.ModelRelationship;
import ytdp.careagle.com.ytdp.entity.ProvinceEntity;
import ytdp.careagle.com.ytdp.entity.PushCarEntity;
import ytdp.careagle.com.ytdp.entity.QueryAllCarType;
import ytdp.careagle.com.ytdp.entity.SellCarProgramEntity;
import ytdp.careagle.com.ytdp.entity.SeriesRelationship;
import ytdp.careagle.com.ytdp.entity.SmsCodeIdEntity;
import ytdp.careagle.com.ytdp.entity.ValuationQuery;
import ytdp.careagle.com.ytdp.entity.VehicleSeries;
import ytdp.careagle.com.ytdp.entity.VersionControlEntity;

/**
 * 哒哒哒哒哒
 */

public class NetWorks extends RetrofitUtils {

    private static NetWorks instance;
    private NetService service;

    /**
     * 构造
     */
    public NetWorks() {
        if (service == null) {
            service = getRetrofit().create(NetService.class);
        }
    }

    /**
     * 单例
     *
     * @return
     */
    public static NetWorks getInstance() {
        if (instance == null) {
            instance = new NetWorks();
        }
        return instance;
    }

    /**
     * 通用订阅
     *
     * @param observable
     * @param observer
     */
    private <T> Disposable setSubscribe(Context context, Observable<T> observable, final Observer<T> observer) {
        return observable.subscribeOn(Schedulers.io())
                .flatMap(new RxHttpResult<T>(context))
                .subscribeOn(Schedulers.newThread())//子线程访问网络
                .observeOn(AndroidSchedulers.mainThread())//回调到主线程
                .subscribe(new Consumer<T>() {
                    @Override
                    public void accept(@NonNull T data) throws Exception {
                        observer.onNext(data);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        observer.onError(throwable);
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        observer.onComplete();
                    }
                });
    }

    private <T> Disposable setSubscribe(Context context, Observable<T> observable, final Observer<T> observer, long timeout) {
        return observable.subscribeOn(Schedulers.io())
                .flatMap(new RxHttpResult<T>(context))
                .subscribeOn(Schedulers.newThread())//子线程访问网络
                .observeOn(AndroidSchedulers.mainThread())//回调到主线程
                .timeout(timeout, TimeUnit.MILLISECONDS)
                .subscribe(new Consumer<T>() {
                    @Override
                    public void accept(@NonNull T data) throws Exception {
                        observer.onNext(data);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        observer.onError(throwable);
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        observer.onComplete();
                    }
                });
    }

    //登录
    public void goLogin(Context context, Map<String, String> map, Observer<Result<LoginEntity>> observer) {
        setSubscribe(context, service.goLogin(map), observer);
    }

    //登出
    public void goLogout(Context context, Map<String, String> map, Observer<Result> observer) {
        setSubscribe(context, service.goLogout(G.getHeader(context), map), observer);
    }

    public void getVerificationCode(Context context, Map<String, String> header, Map<String, String> map, Observer<Result<SmsCodeIdEntity>> observer) {
        setSubscribe(context, service.getVerificationCode(header, map), observer);
    }

    //重置密码
    public void resetPassword(Context context, Map<String, String> header, HashMap<String, String> map, MyObserver<Result> observer) {
        setSubscribe(context, service.resetPassword(header, map), observer);
    }

    //车品牌1
    public Disposable getBrandRelationship(Context context, Map<String, String> header, MyObserver<Results<BrandRelationship>> observer) {
        return setSubscribe(context, service.getBrandRelationship(header), observer);
    }

    //车系
    public Disposable getSeriesRelationship(Context context, Map<String, String> header, Map<String, String> map, MyObserver<Results<SeriesRelationship>> observer) {
        return setSubscribe(context, service.getRelationship(header, map), observer);
    }

    //地址
    public Disposable getLocalProvinceCity(Context context, Map<String, String> header, MyObserver<Results<ProvinceEntity>> observer) {
        return setSubscribe(context, service.getLocalProvinceCity(header), observer);
    }

    //车型
    public Disposable getModelRelationship(Context context, Map<String, String> header, Map<String, String> map, MyObserver<Results<ModelRelationship>> observer) {
        return setSubscribe(context, service.getModelRelationship(header, map), observer);
    }

    //估价查询
    public Disposable queryValuationQuery(Context context, Map<String, String> header, Map<String, String> map, MyObserver<Result<ValuationQuery>> observer) {
        return setSubscribe(context, service.queryValuationQuery(header, map), observer);
    }

    //获取车型配置
    public Disposable findByModelCode(Context context, Map<String, String> header, Map<String, String> map, MyObserver<Result> observer) {
        return setSubscribe(context, service.findByModelCode(header, map), observer);
    }

    //获取导航
    public Disposable getBanner(Context context, Map<String, String> header, MyObserver<Results<VehicleSeries>> observer) {
        return setSubscribe(context, service.getBanner(header), observer);
    }

    //获取车型推荐
    public Disposable findVehicleSeriesByResidualvalue(Context context, Map<String, String> header, Map<String, String> map, MyObserver<Result<PushCarEntity>> observer) {
        return setSubscribe(context, service.findVehicleSeriesByResidualvalue(header, map), observer);
    }

    //获取车系下上架车型
    public Disposable findVehicleByVehicleSeriesAndOnline(Context context, Map<String, String> header, Map<String, String> map, MyObserver<Results<SellCarProgramEntity>> observer) {
        return setSubscribe(context, service.findVehicleByVehicleSeriesAndOnline(header, map), observer);
    }

    //获取基础配置
    public Disposable getBasicConfig(Context context, Map<String, String> header, MyObserver<Result<BaseConfig>> observer) {
        return setSubscribe(context, service.getBasicConfig(header), observer);
    }

    //查看全部车型
    public Disposable listVehicleSeriesByShop(Context context, Map<String, String> header, MyObserver<Results<QueryAllCarType>> observer) {
        return setSubscribe(context, service.listVehicleSeriesByShop(header), observer);
    }

    //获取车型参数详情
    public Disposable getVehicleModelParams(Context context, Map<String, String> header, HashMap<String, String> map, MyObserver<Results<List<String>>> observer) {
        return setSubscribe(context, service.getVehicleModelParams(header, map), observer);
    }

    // 保存客户信息
    public Disposable saveCustomer(Context context, Map<String, String> header, HashMap<String, String> map, MyObserver<Result> observer) {
        return setSubscribe(context, service.saveCustomer(header, map), observer);
    }

    // 获取最新版本信息
    public Disposable getCurrVersion(Context context, HashMap<String, String> map,MyObserver<Result<VersionControlEntity>> observer) {
        return setSubscribe(context, service.getCurrVersion(map), observer);
    }
}
